package cn.sms.order.controller;

import cn.sms.basic.exception.BusinessException;
import cn.sms.basic.util.AjaxResult;
import cn.sms.basic.util.LoginContext;
import cn.sms.basic.util.PageList;
import cn.sms.order.domain.AdoptOrder;
import cn.sms.order.query.AdoptOrderQuery;
import cn.sms.order.service.IAdoptOrderService;
import cn.sms.order.service.IProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/adoptOrder")
public class AdoptOrderController {
    @Autowired
    private IAdoptOrderService iAdoptOrderService;
    @PostMapping("/admin")
    public PageList<AdoptOrder> admin(@RequestBody AdoptOrderQuery query,HttpServletRequest request){
        return iAdoptOrderService.admin(query,LoginContext.getLoginInfo(request));
    }
    @PostMapping("/user")
    public PageList<AdoptOrder> queryUserOrder(@RequestBody AdoptOrderQuery query,HttpServletRequest request){
        return iAdoptOrderService.queryUserOrder(query,LoginContext.getLoginInfo(request));
    }

    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String,Object> params,HttpServletRequest request){
        try {
            String submit = iAdoptOrderService.submit(params, LoginContext.getLoginInfo(request));
            System.out.println(submit);
            return AjaxResult.me().setResultObj(submit);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @GetMapping("/{id}")
    public AdoptOrder queryById(@PathVariable("id") Long id){
        return iAdoptOrderService.queryById(id);
    }
}
